Using OData, update a DateTime field for an entity record. Set the field to the current date and time (with UTC offset)
// Sets a DateTime field's value to the current date/time and updates the record.
updateEntityFieldWithCurrentDateTime: function (entitySchemaName, entityGuid, dateTimeFieldSchemaName, successCallback, errorCallback) {
// This is an example of getting the OData URI. See the CRM SDK for more info.
var uri = getOdataUri();
// Get the current date/time offset for the current time zone.
// This is to store the current date/time to CRM in UTC (the local time minus the time zone offset).
var now = new Date();
var timeZoneOffsetHours = now.getTimezoneOffset() / 60;
var d2 = new Date();
d2.setHours(now.getHours() - timeZoneOffsetHours);
var entityToUpdate = {};
entityToUpdate[dateTimeFieldSchemaName] = d2;
var jsonEntity = JSON.stringify(entityToUpdate);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: uri + "/" + entitySchemaName + "Set(guid'" + entityGuid + "')",
async: false,
beforeSend: function (xmlhttp) {
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: successCallback,
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
},
Using OData, update a DateTime field for an entity record. Set the field to the current date and time (with UTC offset)
// Sets a DateTime field's value to the current date/time and updates the record. updateEntityFieldWithCurrentDateTime: function (entitySchemaName, entityGuid, dateTimeFieldSchemaName, successCallback, errorCallback) { // This is an example of getting the OData URI. See the CRM SDK for more info. var uri = getOdataUri(); // Get the current date/time offset for the current time zone. // This is to store the current date/time to CRM in UTC (the local time minus the time zone offset). var now = new Date(); var timeZoneOffsetHours = now.getTimezoneOffset() / 60; var d2 = new Date(); d2.setHours(now.getHours() - timeZoneOffsetHours); var entityToUpdate = {}; entityToUpdate[dateTimeFieldSchemaName] = d2; var jsonEntity = JSON.stringify(entityToUpdate); $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", data: jsonEntity, url: uri + "/" + entitySchemaName + "Set(guid'" + entityGuid + "')", async: false, beforeSend: function (xmlhttp) { xmlhttp.setRequestHeader("Accept", "application/json"); xmlhttp.setRequestHeader("X-HTTP-Method", "MERGE"); }, success: successCallback, error: function (XmlHttpRequest, textStatus, errorThrown) { alert(errorThrown); } }); },